//by k1 2019.3.29 - 2022.2.17 ***

if (!isNil "k1_un_addEH") exitWith {};

if(isNil "k1_ply_Unconscious")then {k1_ply_Unconscious = false}; //true 对玩家有效

k1_AI_Unconscious = {
	if (damage _this <= .25 || {!(_this getVariable ["k1_Only_Damage_EH", true])}) exitWith {};
	_this setVariable ["k1_Only_Damage_EH", false];
	_this setUnconscious true; //被敌人发现后设置无意识不会改变成平民阵营
	_t = 10 + random 170;
	if (isNil "AI_un_setCaptive" && {random 100 < 60}) then {
		[_this, time + (random _t)] spawn {
			params ["_u","_t"];
			_u setCaptive 1213;
			waitUntil {time > _t || {!alive _u} || {damage _u <= .25} || {!isNull objectParent _u}};
			if (!alive _u) exitWith {};
			_u setCaptive 0;
		};
	};
	_t1 = time + _t;
	waitUntil {time > _t1 || {!alive _this} || {damage _this <= .25} || {!isNull objectParent _this}};
	if (!alive _this) exitWith {};
	_this setUnconscious false;
	_this setVariable ["k1_Only_Damage_EH", nil];
	//没有步枪和手枪会卡无意识动作！
	_move = switch (currentWeapon _this) do {
		case "": {"amovppnemstpsnonwnondnon"};
		case (primaryWeapon _this): {"amovppnemstpsraswrfldnon"};
		case (secondaryWeapon _this): {"amovpknlmstpsraswlnrdnon"};
		case (handGunWeapon _this): {"amovppnemstpsraswpstdnon"};
		default {"amovppnemstpsnonwnondnon"};
	};
	sleep 2;
	if (alive _this && {isNull objectParent _this} && {_this getVariable ["k1_Only_Damage_EH", true]}) then {[_this, _move] remoteExec ["playMoveNow", 0]}; //debug
};

k1_un_addEH = {
	_idx = _this addEventHandler ["HandleDamage", {
		params ["_unit", "", "_amountOfDamage", "", "_projectile"];
		if (local _unit && {!(lifeState _unit in ["INCAPACITATED", "DEAD"])} && {!isPlayer _unit || {k1_ply_Unconscious}} && {isNil {_unit getVariable "FAR_isUnconscious"}} && {isNull objectParent _unit} && {_unit getVariable ["k1_Only_Damage_EH", true]}) then {
			_amountOfDamage = if(_projectile == "")then[{_amountOfDamage},{(damage _unit)+(_amountOfDamage * .17)}]; //
			_head = if(random 100 < 30)then[{(_unit getHit "head") > .25},{false}]; //20
			if (alive _unit && {_head || {_amountOfDamage >= 1}}) then {_unit spawn k1_AI_Unconscious};
			//_amountOfDamage
		};
	}];
	_this setVariable ["AI_Un_EH_idx", _idx];
};

k1_allUnits = {
	private ["_objs", "_obj"];
	_objs = [];
	{
		_obj = agent _x;
		if (alive _obj) then {_objs pushback _obj};
	} forEach agents;
	_objs append allUnits;
	_objs
}; //

addMissionEventHandler ["EntityKilled", {
	_unit = _this select 0;
	_idx = _unit getVariable "AI_Un_EH_idx";
	if (!isNil "_idx") then {
		_unit removeEventHandler ["HandleDamage", _idx];
		_unit setVariable ["AI_Un_EH_idx", nil];
	};
	_unit setVariable ["AI_Unconscious_EH", nil];
	_unit setVariable ["k1_Only_Damage_EH", nil];
}];

addMissionEventHandler ["EntityRespawned", {
	_unit = _this select 0;
	if (local _unit && {captiveNum _unit == 1213}) then {_unit setCaptive 0};
}];

0 spawn {uiSleep 10; systemChat "添加AI无意识，可击昏功能"}; ////

waitUntil {
	{
		if ( _x getVariable ["AI_Unconscious_EH", true] && {alive _x} && {_x isKindOf "CaManBase"} && {toLower typeOf _x find "zombie" == -1} && {!isPlayer _x || {k1_ply_Unconscious}} && {isNil {_x getVariable "FAR_isUnconscious"}} && {!(["UAV_AI", typeOf _x] call BIS_fnc_inString)} ) then {
			_x call k1_un_addEH;				
			_x setVariable ["AI_Unconscious_EH", false];
	 	};
	} forEach (call k1_allUnits); //allUnits 过滤Ryan's Zombies & Demons僵尸
	sleep 30;
	false
};


